<template>
  <div>
    <div class="top">添加客户</div>
    <div style="margin: 20px"></div>
    <el-form
      :label-position="labelPosition"
      label-width="100px"
      :model="formLabelAlign"
      class="form"
    >
      <el-form-item label="客户名称:" prop="customername">
        <el-input v-model="formLabelAlign.cl_name"></el-input>
      </el-form-item>
      <el-form-item label="客户地址:" prop="customeraddress">
        <el-input v-model="formLabelAlign.cl_address"></el-input>
      </el-form-item>
      <el-form-item label="纬度:">
        <el-input v-model="formLabelAlign.cl_latitude"></el-input>
      </el-form-item>
      <el-form-item label="经度:">
        <el-input v-model="formLabelAlign.cl_longitude"></el-input>
      </el-form-item>
      <el-form-item label="客户类型:">
        <el-select v-model="formLabelAlign.cl_type">
          <el-option
            v-for="(item, index) in custtypeList"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="价格体系:">
        <el-select v-model="formLabelAlign.cl_Price_system">
          <el-option
            v-for="item in priceList"
            :key="item.id"
            :label="item.priceName"
            :value="item.priceName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="营业面积:" prop="businessarea">
        <el-input v-model="formLabelAlign.cl_leaderperson"></el-input>
      </el-form-item>
      <el-form-item label="联系人:" prop="contact">
        <el-input v-model="formLabelAlign.cl_contactperson"></el-input>
      </el-form-item>
      <el-form-item label="手机号码:" prop="phonenumber">
        <el-input v-model="formLabelAlign.cl_phone"></el-input>
      </el-form-item>
      <el-form-item label="固话:" prop="fixed">
        <el-input v-model="formLabelAlign.cl_Landline_telephones"></el-input>
      </el-form-item>
      <el-form-item label="负责人员:">
        <el-input v-model="formLabelAlign.cl_creatyperson"></el-input>
      </el-form-item>
      <el-form-item label="区域名称:">
        <el-input v-model="formLabelAlign.cl_area"></el-input>
      </el-form-item>
      <el-form-item label="客户图片:">
        <el-upload
          class="avatar-uploader"
          action=""
          :show-file-list="false"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addCust('numberValidateForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { AddCustApi, EditCustApi } from "@/http/merchant/customerInfo";
import { getPriceCount } from "@/http/priceApi/priceApi";
import { getCustAllApi } from "@/http/merchant/custType";
export default {
  data() {
    let CustomerName = (rule, value, callback) => {
      if (/[\u4e00-\u9fa5]+$/.test(value)) {
        return callback();
      }
      callback(new Error("请输入正确的名字"));
    };
    let CustomerAddress = (rule, value, callback) => {
      if (/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/.test(value)) {
        return callback();
      }
      callback(new Error("请输入正确的地址"));
    };
    let Businessarea = (rule, value, callback) => {
      if (/^[a-zA-Z0-9_]+$ /.test(value)) {
        return callback();
      }
      callback(new Error("请输入正确的数字"));
    };
    let Contact = (rule, value, callback) => {
      if (/[\u4e00-\u9fa5]/.test(value)) {
        return callback();
      }
      callback(new Error("请输入正确的名字"));
    };
    let Phonenumber = (rule, value, callback) => {
      if (
        /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(
          value
        )
      ) {
        return callback();
      }
      callback(new Error("请输入正确的电话号码"));
    };
    let Fixed = (rule, value, callback) => {
      if (
        /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(
          value
        )
      ) {
        return callback();
      }
      callback(new Error("请输入正确的电话号码"));
    };
    return {
      labelPosition: "right",
      formLabelAlign: {
        cl_Landline_telephones: "",
        cl_Price_system: "",
        cl_address: "",
        cl_area: "",
        cl_contactperson: "",
        cl_createtime: "",
        cl_creatyperson: "",
        cl_latitude: "",
        cl_leaderperson: "",
        cl_longitude: "",
        cl_name: "",
        cl_phone: "",
        cl_type: "",
        cl_image: "",
        cl_id:""
      },
      priceList: [],
      custtypeList: [],
      imageUrl: "",
      rules: {
        customername: [
          { required: true, message: "请输入正确的名字", trigger: "blur" },
          { validator: CustomerName, trigger: "blur" },
        ],
        customeraddress: [
          { required: true, message: "请输入正确的地址", trigger: "blur" },
          { validator: CustomerAddress, trigger: "blur" },
        ],
        businessarea: [
          { required: true, message: "请输入正确的数字", trigger: "blur" },
          { validator: Businessarea, trigger: "blur" },
        ],
        contact: [
          { required: true, message: "请输入正确的名字", trigger: "blur" },
          { validator: Contact, trigger: "blur" },
        ],
        phonenumber: [
          { required: true, message: "请输入正确的电话号码", trigger: "blur" },
          { validator: Phonenumber, trigger: "blur" },
        ],
        fixed: [
          { required: true, message: "请输入正确的电话号码", trigger: "blur" },
          { validator: Fixed, trigger: "blur" },
        ],
      },
    };
  },
  async activated() {
    if (this.$route.query.row) {
      this.formLabelAlign = this.$route.query.row;
    }
    Promise.all([await getPriceCount(), await getCustAllApi()])
      .then((res) => {
        this.priceList = res[0].data.data;
        this.custtypeList = [
          ...new Set(
            res[1].data.data.map((v) => v.ctm_father_type).filter(Boolean)
          ),
        ];
      })
      .catch((err) => {
        console.log(err);
      });
  },
  deactivated(){
   this.formLabelAlign={
        cl_Landline_telephones: "",
        cl_Price_system: "",
        cl_address: "",
        cl_area: "",
        cl_contactperson: "",
        cl_createtime: "",
        cl_creatyperson: "",
        cl_latitude: "",
        cl_leaderperson: "",
        cl_longitude: "",
        cl_name: "",
        cl_phone: "",
        cl_type: "",
        cl_image: "",
        cl_id:""
      }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      if (isJPG && isLt2M) {
        this.formLabelAlign.cl_image = new Blob([file]);
      }
    },
    async addCust() {
      let data;
      try {
        if (this.$route.query.row) {
          data = await EditCustApi({
            ...this.formLabelAlign,
             cl_createtime:new Date().toLocaleDateString()+" "+new Date().toLocaleTimeString()
          });
        } else {
          data = await AddCustApi({
             ...this.formLabelAlign,
             cl_createtime:new Date().toLocaleDateString()+" "+new Date().toLocaleTimeString()
          });
        }
        console.log(data)
        if (data.data.code != 200) throw data.data;
        this.$message.success("操作成功");
        this.$router.go(-1);
      } catch (e) {
        this.$message.error(e.msg||"操作失败");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  padding: 0 20px;
  box-sizing: border-box;
  width: 100%;
  height: 50px;
  background-color: #eee;
  border: 1px solid #ddd;
  border-bottom: none;
  line-height: 50px;
}

.form {
  margin: auto;
  width: 400px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>